package com.ebs.md.impl.payload.hit;

/**
 * Perforce Revision:
 *  $File: //depot/MarketData/commons-md-payload-impl/MAIN/src/main/java/com/ebs/md/impl/payload/hit/BasicHitSubmitAdapterImpl.java $
 *  $Revision: #3 $  $Change: 115479 $  $DateTime: 2012/10/09 13:50:53 $  $Author: Arik.Sher $
 */

import java.util.Calendar;
import java.util.TimeZone;

import org.apache.log4j.Logger;

import com.ebs.commons.comp.IParsedPayload;
import com.ebs.md.api.Rate;
import com.ebs.md.api.abs.AbsProductProvider;
import com.ebs.md.api.hit.BasicHit;
import com.ebs.md.api.hit.HitSide;
import com.ebs.md.api.hit.IHitSubmit;
import com.ebs.md.api.hit.IHitSubmitAdapter;
import com.ebs.md.api.product.PairProduct;


public class BasicHitSubmitAdapterImpl
implements       IHitSubmitAdapter<IParsedPayload, IHitSubmit>
{
    protected final Logger  logger = Logger.getLogger(getClass());

	static final protected TimeZone gmtTZ = TimeZone.getTimeZone("GMT");

	protected AbsProductProvider<String, PairProduct> pairProductProvider;

	public void setPairProductProvider(AbsProductProvider<String, PairProduct> pairProductProvider) {
		this.pairProductProvider = pairProductProvider;
	}

	protected PairProduct extractProduct(IParsedPayload pp) {
		String  baseCcyIso  = pp.getParsedStringField ( "base_ccy");
		String localCcyIso  = pp.getParsedStringField ("local_ccy");
		int    matchingType = pp.getParsedIntField    ("matching_type");
		
		String productKey = PairProduct.generateKey(baseCcyIso, localCcyIso, matchingType);
		
		PairProduct cachedProduct = pairProductProvider.getProduct(productKey);
		
		if ( cachedProduct == null ) {
			// caching it
			cachedProduct = new PairProduct(baseCcyIso, localCcyIso, matchingType);
			
			pairProductProvider.storeKnownItem(productKey, cachedProduct);
		}
		return cachedProduct;
	}

	protected HitSide extractSide(IParsedPayload pp) {
		return HitSide.byLegcayCode(pp.getParsedIntField("qte_type"));
	}

	public IHitSubmit convert(IParsedPayload source) {
		String mqMsgType = source.getParsedStringField("mq_msg_type");
		//    RHST = HITSUBMIT
		if ( "RHST".equals(mqMsgType) ) {
			PairProduct product = extractProduct(source);
			HitSide     side    = extractSide(source);
			long        amount  = source.getParsedLongField  ("qte_amt_result");
			String      region  = source.getParsedStringField("trader_region");
			Rate        hitRate = new Rate(
					              source.getParsedStringField("prc_qte_value", "0.0"));

			String hitId    = source.getParsedStringField("ebs_trans_id");
			String dealCode = source.getParsedStringField("qte_floor_code");
			String traderId = source.getParsedStringField("trader_id");
			long   msgTime  = source.getParsedLongField  ("msg_timestamp_long", 0);
			
			Calendar hitTime = Calendar.getInstance(gmtTZ);
			hitTime.setTimeInMillis(msgTime);

			BasicHit bh = new BasicHit(product, side, amount, hitRate
					, hitId, hitTime, dealCode, traderId, region);
			return bh;
		}
		return null;
	}

}
